﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySQLQueryMonitor.Util;
using PcapDotNet.Core;
using System.Windows;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Collections.ObjectModel;
using System.Windows.Threading;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Markup;
using Microsoft.Win32;
using System.IO;

namespace MySQLQueryMonitor.ViewModel
{
    public class MainViewModel : NotificationObject
    {
        public MainViewModel()
        {
            Devices = LivePacketDevice.AllLocalMachine;
            FontFamilies = new FontFamilyViewModel();

            FontFamily = new FontFamily("Consolas");
            FontSize = 15.0;
            FontColor = Colors.Red;

            // Bind command to its handlers
            ClearLogCommand = new RelayCommand(ClearLogCommandImpl);
            SaveLogCommand  = new RelayCommand(SaveLogCommandImpl);
            AboutCommand    = new RelayCommand(AboutCommandImpl);

            Model = new MainModel()
            {
                Device = Devices.FirstOrDefault()
            };
        }

        #region private
        private FontFamily fontFamily;
        private double fontSize;
        private Color fontColor;
        #endregion

        #region property
        public MainModel Model { get; private set; }
        public ReadOnlyCollection<LivePacketDevice> Devices { get; private set; }
        public IEnumerable<GroupedItem<FontFamily>> FontFamilies { get; private set; }
        public FontFamily FontFamily
        {
            get { return fontFamily; }
            set { fontFamily = value; RaisePropertyChanged(() => FontFamily); }
        }
        public IEnumerable<double> FontSizes { get; private set; }
        public double FontSize
        {
            get { return fontSize; }
            set { fontSize = value; RaisePropertyChanged(() => FontSize); }
        }
        public Color FontColor
        {
            get { return fontColor; }
            set { fontColor = value; RaisePropertyChanged(() => FontColor); }
        }
        #endregion

        #region command
        public ICommand ClearLogCommand { get; private set; }
        public ICommand SaveLogCommand { get; private set; }
        public ICommand AboutCommand { get; private set; }
        #endregion

        #region command implementation
        private void ClearLogCommandImpl(object param)
        {
            Model.CommandLog.Clear();
        }
        private void SaveLogCommandImpl(object param)
        {
            SaveFileDialog dialog = new SaveFileDialog()
            {
                DefaultExt = ".txt",
                Filter = "Text document (*.txt)|*.txt"
            };
            if (dialog.ShowDialog() == true)
            {
                using (var stream = dialog.OpenFile())
                using (var writer = new StreamWriter(stream))
                {
                    foreach (var log in Model.CommandLog)
                    {
                        writer.WriteLine("{0}\t{1}", log.TimeStamp, log.Statement);
                    }
                }
            }
        }
        private void AboutCommandImpl(object param)
        {
            MessageBox.Show("MySQL Packet Monitor version 0.1\nCopyright ", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
        }
        #endregion
    }
}

